package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.crm.entity.GwauT;
import cn.e4j.modular.crm.entity.GwaulT;
import cn.e4j.modular.crm.mapper.GwauTMapper;
import cn.e4j.modular.crm.service.GwauTService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  商机分类 服务实现类
 * </p>
 *
 * @author mengyb
 * @since 2019-04-12
 */
@Service
public class GwauTServiceImpl extends ServiceImpl<GwauTMapper, GwauT> implements GwauTService {

    @Transactional(readOnly = true)
    @Override
    public List<Map<String, Object>> getList(Dto dto, Page<Object> page) {

        ShiroUser shiroUser = ShiroKit.getUser();

        dto.put("ent", shiroUser.getDBC());
        dto.put("lang", LangUtils.getLang(shiroUser.getLANG()));

        return this.baseMapper.getList(dto, page);

    }

    @Transactional
    @Override
    public void add(GwauT gwauT, GwaulT gwaulT) {

        ShiroUser shiroUser = ShiroKit.getUser();

        gwauT.setGWAUENT(shiroUser.getDBC());
        gwauT.setGWAU002(" ");
        gwauT.setGWAU003(" ");
        gwauT.setGWAUDATE(LocalDate.now());
        gwauT.setGWAUTIME(DateUtils.formatDate(new Date(), "HH:mm:ss"));
        gwauT.setGWAUUSER(shiroUser.getACCOUNT());
        gwauT.setGWAUGRUP(shiroUser.getORG_ID());
        gwauT.setGWAUORIU(shiroUser.getACCOUNT());
        gwauT.setGWAUORIG(shiroUser.getORG_ID());

        gwaulT.setGWAULENT(shiroUser.getDBC());
        gwaulT.setGWAUL001(gwauT.getGWAU001());
        gwaulT.setGWAUL002(LangUtils.getLang(shiroUser.getLANG()));

        gwauT.insert();
        gwaulT.insert();
    }

    @Transactional
    @Override
    public void edit(GwauT gwauT, GwaulT gwaulT) {

        ShiroUser shiroUser = ShiroKit.getUser();

        gwauT.setGWAUMODU(shiroUser.getACCOUNT());

        UpdateWrapper<GwauT> gwauTUpdateWrapper = new UpdateWrapper<>();
        gwauTUpdateWrapper.eq("GWAUENT", gwauT.getGWAUENT());
        gwauTUpdateWrapper.eq("GWAU001", gwauT.getGWAU001());

        UpdateWrapper<GwaulT> gwaulTUpdateWrapper = new UpdateWrapper<>();
        gwaulTUpdateWrapper.eq("GWAULENT", gwauT.getGWAUENT());
        gwaulTUpdateWrapper.eq("GWAUL001", gwauT.getGWAU001());


        gwauT.update(gwauTUpdateWrapper);
        gwaulT.update(gwaulTUpdateWrapper);

    }

    @Transactional
    @Override
    public void delRow(GwauT gwauT, GwaulT gwaulT) {

        UpdateWrapper<GwauT> gwauTUpdateWrapper = new UpdateWrapper<>();
        gwauTUpdateWrapper.eq("GWAUENT", gwauT.getGWAUENT());
        gwauTUpdateWrapper.eq("GWAU001", gwauT.getGWAU001());

        UpdateWrapper<GwaulT> gwaulTUpdateWrapper = new UpdateWrapper<>();
        gwaulTUpdateWrapper.eq("GWAULENT", gwauT.getGWAUENT());
        gwaulTUpdateWrapper.eq("GWAUL001", gwauT.getGWAU001());

        gwauT.delete(gwauTUpdateWrapper);
        gwaulT.delete(gwaulTUpdateWrapper);

    }

    @Transactional
    @Override
    public void delRows(List<GwauT> gwauTList) {
        for (GwauT gwauT : gwauTList) {
            UpdateWrapper<GwauT> gwauTUpdateWrapper = new UpdateWrapper<>();
            gwauTUpdateWrapper.eq("GWAUENT", gwauT.getGWAUENT());
            gwauTUpdateWrapper.eq("GWAU001", gwauT.getGWAU001());

            GwaulT gwaulT = new GwaulT();
            UpdateWrapper<GwaulT> gwaulTUpdateWrapper = new UpdateWrapper<>();
            gwaulTUpdateWrapper.eq("GWAULENT", gwauT.getGWAUENT());
            gwaulTUpdateWrapper.eq("GWAUL001", gwauT.getGWAU001());

            gwauT.delete(gwauTUpdateWrapper);
            gwaulT.delete(gwaulTUpdateWrapper);
        }
    }

    /**
     * 查重
     * @param gwauT
     * @return
     */
    @Transactional(readOnly = true)
    @Override
    public Boolean existedTheParams(GwauT gwauT) {
        QueryWrapper<GwauT> gwauTQueryWrapper = new QueryWrapper<>();
        gwauTQueryWrapper.eq("GWAUENT", gwauT.getGWAUENT());
        gwauTQueryWrapper.eq("GWAU001", gwauT.getGWAU001());
        return this.baseMapper.selectList(gwauTQueryWrapper).size() > 0;
    }
}
